package cloud.lysky.java.on.concurrent.thread;

/**
 * @author luoly
 * @describe
 * @date 2022-10-18 18:54
 **/
public class StopThreadTest implements Runnable {
    @Override
    public void run() {
        int count = 0;
        while (!Thread.currentThread().isInterrupted() && count < 1000) {
            System.out.println("count=" + count++);

            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
                /*处于休眠中的线程被中断，线程是可以感受到中断信号的，并且会抛出一个
                InterruptedException 异常，同时清除中断信号，将中断标记位设置成 false。这样就会导致
                while条件Thread.currentThread().isInterrupted()为false*/

                //重新设置线程中断状态为true
                Thread.currentThread().interrupt();
            }
        }
        System.out.println("Thread stop!");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new StopThreadTest());
        thread.start();
        Thread.sleep(5);
        thread.interrupt();
    }
}